package com.xbh.management.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbh.common.web.core.Result;
import com.xbh.management.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-10-05 17:26
 */

@RestController
@RequestMapping(path = "/rolemenu")
public class RoleMenuController {

    @Autowired
    private RoleMenuService roleMenuService;

    @PostMapping("/addRoleRelationMenu")
    public Result addRoleRelationMenu(@RequestBody JSONObject params){
        JSONArray menuIds = params.getJSONArray("menuIds");
        //获取前端所有已选择的菜单列表
        List<Object> menuIdList = menuIds.stream().collect(Collectors.toList());
        Long roleId = params.getLong("roleId");
        int isFlag= roleMenuService.addRoleRelationMenu(menuIdList, roleId);
        if(isFlag > 0){
            return Result.success();
        }
        return Result.failed();
    }
}
